<template lang="pug">
  .CreateBasicDataTypeModal
    Modal(
      v-model="isShowCreateBasicDataTypeModal"
      :title="createBasicDataTypeModalTitle"
      class-name="vertical-center-modal"
      width="680"
    )
      Form.ivu-row(ref="formCreate" :model="formCreate" :rules="rules" :label-width="100")
        FormItem.form-item.ivu-col(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
          template(v-if="item.type === 'text'")
            .line-h-30 {{formCreate[item.model]}}
          template(v-if="item.type === 'input'")
            Input(v-model="formCreate[item.model]" :placeholder="item.placeholder")
          template(v-if="item.type === 'select'")
            Select(v-model="formCreate[item.model]" :placeholder="item.placeholder" @on-change="listenCall(item.fn, formCreate[item.model])")
              Option(v-for="(c_item, c_index) of arrListDown[item.options]" :key="c_index" :value="c_item.value") {{c_item.label}}
          template(v-if="item.type === 'switch'")
            i-switch(v-model="formCreate[item.model]")
      .clearfix(slot="footer")
        Button(type="text" @click="cancel") 取消
        Button(type="primary" @click="handleSave") 保存
</template>

<script>
import { Common } from '@/utils/common.js'
import { SysBasedataTypeApi } from '@/api/SysBasedataType.api.js'
export default {
  name: 'CreateBasicDataTypeModal',

  watch: {
  },

  data () {
    return {
      isShowCreateBasicDataTypeModal: false,
      createBasicDataTypeModalTitle: '',
      basicDataTypeId: '',
      formCreate: {
        name: '',
        code: '',
        type: '',
        allowEmpty: true,
        enabled: true
      },
      formItem: [
        { type: 'input', label: '数据类型', placeholder: '请选择数据类型', prop: 'name', model: 'name', style: 'ivu-col-span-12' },
        { type: 'input', label: '编码', placeholder: '请输入编码', prop: 'code', model: 'code', style: 'ivu-col-span-12' },
        { type: 'select', label: '类型', placeholder: '请选择类型', prop: 'type', model: 'type', style: 'ivu-col-span-12', options: 'datatype', fn: 'selectChange' },
        { type: 'switch', label: '允许为空', placeholder: '', prop: 'allowEmpty', model: 'allowEmpty', style: 'ivu-col-span-12' },
        { type: 'switch', label: '启用', placeholder: '', prop: 'enabled', model: 'enabled', style: 'ivu-col-span-24' }
      ],
      rules: {
        name: [
          { required: true, message: '请选择数据类型', trigger: 'select' }
        ],
        code: [
          { required: true, message: '请输入编码', trigger: 'select' }
        ]
        // enabled: [
        //   { required: true, message: '请选择是否启用', trigger: 'select' }
        // ]
      },
      arrListDown: {
        datatype: []
      }
    }
  },

  mounted () {
    this.getEnum()
  },

  methods: {
    // 数据字典
    getEnum () {
      let groupName = 'common.datatype'
      Common.getEnum(groupName, this.arrListDown)
      // console.log('数据字典：', this.arrListDown)
    },

    // 监听所有调用的方法
    listenCall (methodsWords, param) {
      this[methodsWords](param)
    },

    cancel () {
      this.isShowCreateBasicDataTypeModal = false
    },

    show () {
      this.isShowCreateBasicDataTypeModal = true
      this.$refs.formCreate.resetFields()
    },

    // 根据ID获取基础数据
    async getModel (id) {
      let res = await SysBasedataTypeApi.getModel(id)
      console.log('根据ID获取基础数据: ', res)
      // Object.keys(res.data.data).forEach(key => {
      //   this.arrListDown[key] = res.data.data[key]
      // })
      if (id) {
        Object.keys(res.data.data).forEach(key => {
          this.formCreate[key] = res.data.data[key]
        })
      }
    },

    // 基础数据类型改变
    async typeChange (params) {
      let res = await SysBasedataTypeApi.typeChange(params)
      console.log('基础数据类型改变: ', res)
      if (res.status === 200 && res.data.success) {
        Object.keys(res.data.data).forEach(key => {
          this.arrListDown[key] = res.data.data[key]
        })
      }
    },

    // 数据类型改变
    handleTypeChange (value) {
      var params = {}
      this.arrListDown.typeList.map(item => {
        if (item.key === value) {
          params.id = this.basicDataTypeId
          params.TypeGUID = item.key
          this.typeChange(params)
        }
      })
    },

    selectChange (value) {
      console.log('value: ', value)
    },

    // 保存基础数据类型
    handleSave () {
      console.log('保存基础数据类型：', this.formCreate)
      this.$refs.formCreate.validate((valid) => {
        if (valid) {
          SysBasedataTypeApi.save(this.formCreate).then(res => {
            // console.log('保存基础数据类型res: ', res)
            if (res.status === 200 && res.data.success) {
              this.$Message.success(res.data.message)
              this.isShowCreateBasicDataTypeModal = false
              this.$emit('handleSave')
            } else {
              this.$Message.error({
                content: res.data.message,
                duration: 10,
                closable: true
              })
            }
          })
        } else {
          this.$Message.warning('输入有误，请返回检查!')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>

</style>
